package com.jxb.visibility.forkjoin;

import java.util.concurrent.RecursiveTask;

/**
 * 类注释
 *
 * @author jiaobo
 * @date Created in 2024/12/4 20:42
 **/
public class LongSum extends RecursiveTask<Long> {

    // 任务拆分最小阈值
    static final int SEQUENTIAL_THRESHOLD = 10000;

    int low;
    int high;
    int[] array;

    LongSum(int[] arr, int lo, int hi) {
        array = arr;
        low = lo;
        high = hi;
    }

    @Override
    protected Long compute() {
        //当任务拆分到小于等于阈值时开始求和
        if (high - low <= SEQUENTIAL_THRESHOLD) {
            long sum = 0;
<<<<<<< HEAD
            for (int i = low; i<high; ++i) {
                sum += array[i];
=======
            for (int i = low; i < high; ++i) {
                sum+=array[i];
>>>>>>> dd9d996e1241861cc8f1c378e77b2cae92d56379
            }
            return sum;
        }else {
            //任务过大，继续拆分
<<<<<<< HEAD

        }
        return null;
=======
            int mid = low + (high - low) / 2;
            LongSum left = new LongSum(array, low, mid);
            LongSum right = new LongSum(array, mid, high);
            //提交任务
            left.fork();
            right.fork();
            //获取任务的执行结果
            long rightAns = right.join();
            long leftAns = left.join();
            return leftAns + rightAns;
        }
>>>>>>> dd9d996e1241861cc8f1c378e77b2cae92d56379
    }
}
